/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.service;

import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cmcc.zysoft.infocollection.mobile.dao.MReadNewsDao;
import com.cmcc.zysoft.infocollection.model.ReadNews;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: ReadNewsService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-12 上午11:27:17
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class MReadNewsService extends BaseServiceImpl<ReadNews, String> {

	/**
	 * 日志.
	 */
	private static final Logger logger = LoggerFactory.getLogger(MReadNewsService.class);
	
	@Resource
	private MReadNewsDao mReadNewsDao;
	
	/**
	 * 属性名称: mNewsService.
	 * 类       型: MNewsService.
	 */
	@Resource
	private MNewsService mNewsService;
	
	/**
	 * 属性名称: mSystemUserService.
	 * 类       型: MSystemUserService.
	 */
	@Resource
	private MSystemUserService mSystemUserService;
	
	@Override
	public HibernateBaseDao<ReadNews, String> getHibernateBaseDao() {
		return this.mReadNewsDao;
	}
	
	/**
	 * 设置通知的已读状态或者更新阅读次数.
	 * @param newsId 新闻ID
	 * @param usercode 手机号
	 * @return map
	 */
	@Transactional
	public void read(String newsId, String usercode) {
		ReadNews readNews = null;
		List<SystemUser> list1 = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
		if(!list1.isEmpty()) {
			String userId = list1.get(0).getUserId();
			try {
				List<ReadNews> list = this.mReadNewsDao.findByNamedParam(new String[]{"newsId", "userId"}, new Object[]{newsId, userId});
				if(list.isEmpty()) { //第一次阅读
					readNews = new ReadNews();
					readNews.setNewsId(newsId);
					readNews.setUserId(userId);
					readNews.setReadDate(new Date());
					readNews.setReadNum(1);
					this.insertEntity(readNews);
				} else { //第n(n>1)次阅读-更新阅读次数
					readNews = list.get(0);
					readNews.setReadNum(readNews.getReadNum() + 1);
					this.updateEntity(readNews);
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.debug("#########  阅读标识添加失败!  #########");
			}
		} else {
			logger.debug("#########  用户不存在或已删除!  #########");
		}
	}
}